//
//  MSCollectionViewController.m
//  MSJD
//
//  Created by xiangbo on 2018/9/24.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCollectionViewController.h"

#import "MSPhoneLoginController.H"

#import "MSCollectHeadView.h"
#import "MSCollectDetailView.h"
#import "MSEmptyView.h"

@interface MSCollectionViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) MSEmptyView *emptyView;

@property (nonatomic, strong) MSCollectHeadView *headView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) MSCollectDetailView *collectHotelView;
@property (nonatomic, strong) MSCollectDetailView *collectStoreView;

@end

@implementation MSCollectionViewController {
    NSInteger _showIndex; // 当前正在展示的界面 0-酒店  1-店铺
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"收藏";
    self.view.backgroundColor = rgba(242,242,242,1.0);
    _showIndex = -1;
    
    [self initSubViews];
    [self initLoginStatusNotifications];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_showIndex >= 0) {
        if (self.collectHotelView) {
            [self.collectHotelView refreshCollectList];
        }
        if (self.collectStoreView) {
            [self.collectStoreView refreshCollectList];
        }
    }
    if (_showIndex == -1) {
        _showIndex = 0;
    }
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - Init Method

- (void)initSubViews {
    if ([UserAccesser hadDefaultAccount]) {
        // 已登录
        [self showCollectDetailView];
    } else {
        [self showLoginTipView];
    }
}
/** 展示收藏夹界面 */
- (void)showCollectDetailView {
    // 添加头部控件
    CGFloat headViewHeight = 36.0;
    [self.view addSubview:self.headView];
    _WEAKSELF
    self.headView.HotelButtonClickBlock = ^(NSInteger type) {
        [weakSelf headViewSelectButtonChanged:type];
    };
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(headViewHeight);
    }];
    
    // 添加滚动控件scrollView
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    // 添加酒店列表界面
    CGFloat detailViewHeight = MS_SCREEN_HEIGHT - headViewHeight - 49.0 - 64.0;
    MSCollectDetailView *collectDetailView = [[MSCollectDetailView alloc] initWithFrame:CGRectZero type:MSCollectDetailViewTypeFavoriteHotel];
    collectDetailView.frame = CGRectMake(0, 0, MS_SCREEN_WIDTH, detailViewHeight);
    [self.scrollView addSubview:collectDetailView];
    self.collectHotelView = collectDetailView;
    
    // 添加店铺列表界面
    MSCollectDetailView *collectStoreView = [[MSCollectDetailView alloc] initWithFrame:CGRectZero type:MSCollectDetailViewTypeFavoriteStore];
    collectStoreView.frame = CGRectMake(MS_SCREEN_WIDTH, 0, MS_SCREEN_WIDTH, detailViewHeight);
    [self.scrollView addSubview:collectStoreView];
    self.collectStoreView = collectStoreView;
    
    self.scrollView.contentSize = CGSizeMake(MS_SCREEN_WIDTH * 2, headViewHeight);
}

/** 初始化登录提示界面 */
- (void)showLoginTipView {
    MSEmptyView *emptyView = [MSEmptyView loadEmptyViewWithType:MSEmptyTypeLoginTip toast:@"登录之后才会展示收藏列表" frame:CGRectMake(0, -40, MS_SCREEN_WIDTH, MS_SCREEN_HEIGHT)];
    // 重试按钮点击
    _WEAKSELF
    emptyView.RetryButtonClickBlock = ^{
        [weakSelf showLoginViewController];;
    };
    [self.view addSubview:emptyView];
    self.emptyView = emptyView;
}
/** 监听登录状态的变化 */
- (void)initLoginStatusNotifications {
    // FIXME:通知名未定义好
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLoginSuccessNotification) name:MSUserLoginSuccessNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLogOutNotification) name:MSUserLogOutNotification object:nil];
}

#pragma mark - Notifications
/** 监听登录成功的消息 */
- (void)receiveLoginSuccessNotification {
    if (self.emptyView) {
        [self.emptyView removeFromSuperview];
        self.emptyView = nil;
    }
    [self showCollectDetailView];
}
/** 监听退出登录的消息 */
- (void)receiveLogOutNotification {
    if (self.headView) {
        [self.headView removeFromSuperview];
        self.headView = nil;
    }
    if (self.scrollView) {
        [self.scrollView removeFromSuperview];
        self.scrollView = nil;
    }
    if (self.emptyView) {
        return;
    }
    [self showLoginTipView];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / MS_SCREEN_WIDTH;
    if (_showIndex == -1) {
        _showIndex = 0;
    }
    if (_showIndex == index) {
        // 滚动结束之后未刷新到新的界面
        return;
    }
    _showIndex = index;
    [self.headView changeHeadViewSelectButton:index];
    if (index == 1) {
        // 收藏
        [self.collectStoreView firstShow];
    } else {
        // 滚动到播放界面
        [self.collectStoreView firstShow];
    }
}

#pragma mark - Event Response

- (void)showLoginViewController {
    MSPhoneLoginController *phoneLoginVC = [[MSPhoneLoginController alloc] init];
    MSNavigationController *nav = [[MSNavigationController alloc] initWithRootViewController:phoneLoginVC];
    [self presentViewController:nav animated:YES completion:nil];
    if (self.emptyView) {
        [self.emptyView resetRetryButtonStatus];
    }
}

#pragma mark - Private Method
/** 头部控件的选中按钮修改 */
- (void)headViewSelectButtonChanged:(NSInteger)index {
    [self.collectStoreView firstShow];
    _WEAKSELF
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.scrollView.contentOffset = CGPointMake(index * MS_SCREEN_WIDTH, 0.0);
    }];
}

#pragma mark - Getters

- (MSCollectHeadView *)headView {
    if (!_headView) {
        _headView = [[MSCollectHeadView alloc] init];
    }
    return _headView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
        _scrollView.pagingEnabled = YES;
        _scrollView.scrollEnabled = NO;
        _scrollView.userInteractionEnabled = YES;
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsHorizontalScrollIndicator = YES;
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
        
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _scrollView;
}

@end
